<?php include("../../includes/includes.php"); ?>
<?php
$trade = getTrade($_GET['tradeId']);
connect($config);
$partyTwoPicks = getPicks($trade->partyTwoId, false, $league);
$partyTwoPlayers = getDraftedPlayers($trade->partyTwoId);

$partyOneTeam = getTeamById($trade->partyOneId, false);
$partyTwoTeam = getTeamById($trade->partyTwoId, false);

$partyOnePicks = getPicks($trade->partyOneId, false);
$partyOnePlayers = getDraftedPlayers($trade->partyOneId);

$result = "<form action='../actions/proposeTrade.php' method='post' target='hiddenFrame'><input type='hidden' name='partyTwo' value='$partyTwo'><table width='30%' align='center'>";
$result .= "<tr><td width='40%'><table><tr><td><b>" . $loggedInTeam->coachName . "</b></td></tr><tr><td><b>Players</b></td></tr>";
$result .= "<tr><td><select id='partyOnePlayers' size='10' multiple>";
$count = count($partyOnePlayers);
$count2 = count($trade->partyTwoReceives);
for ($i = 0; $i < $count; $i++) {
        $foundIt = false;
        for ($k = 0; k < $count2; k++) {
           if ($trade->partyTwoReceives[$k]->itemType == TradeItemType::Player && $trade->partyTwoReceives[$k]->player->id == $partyOnePlayers[$i]->player->id) {
              $foundIt = true;
              break;
           }
        }
        if (!foundIt) { 
	   $result .= "<option value='" . $partyOnePlayers[$i]->player->id . "'>" . $partyOnePlayers[$i]->player->getDescription() . "</option>";  
        }
}
$result .= getSpacerOption() . "</select></td></tr><tr><td><b>Picks</b></td></tr><tr><td><select id='partyOnePicks' size='10' multiple style='width: 80'>";
$count = count($partyOnePicks);
for ($i = 0; $i < $count; $i++) {
        $foundIt = false;
        for ($k = 0; k < $count2; k++) {
           if ($trade->partyTwoReceives[$k]->itemType == TradeItemType::Pick && $trade->partyTwoReceives[$k]->pickNumber == $partyOnePicks[$i]->pickNumber) {
              $foundIt = true;
              break;
           }
        }
        if (!foundIt) { 
 	   $result .= "<option value='" . $partyOnePicks[$i]->pickNumber . "'>#" . $partyOnePicks[$i]->pickNumber . "</option>";  
        }
}
$result .= getSpacerOption() . "</select></td></tr><tr><td><b>Receives</b></td></tr><tr><td><select id='partyOneReceives' size='10' multiple>";
$count = count($trade->partyOneReceives);
for ($i = 0; $i < $count; $i++) {
   if ($trade->partyOneReceives[$i]->itemType == TradeItemType::Pick) {
      $result .= "<option value='" . $trade->partyOneReceives[$i]->pickNumber . "'>#" . $trade->partyOneReceives[$i]->pickNumber . "</option>";  
   }
   else {
      $result .= "<option value='" . $partyOneReceives[$i]->player->id . "'>" . $partyOneReceives[$i]->player->getDescription() . "</option>";  
   }
}
$result .= getSpacerOption() . "</select></td></tr></table></td>";

$result .= "<td width='20%'><table><tr><td align='center'><br><br><br><br><br><br><br><input type='button' value='Trade to -->' onclick='tradeTo(this.form)'></td></tr><tr><td><br><br></td></tr><tr><td align='center'><input type='button' value='<-- Trade for' onclick='tradeFor(this.form)'></td></tr></table></td>";

$result .= "<td width='40%'><table><tr><td><b>" . $partyTwoTeam->coachName . "</b></td></tr><tr><td><b>Players</b></td></tr>";
$result .= "<tr><td><select id='partyTwoPlayers' size='10' multiple>";
$count = count($partyTwoPlayers);
$count2 = count($trade->partyOneReceives);
for ($i = 0; $i < $count; $i++) {
        $foundIt = false;
        for ($k = 0; k < $count2; k++) {
           if ($trade->partyOneReceives[$k]->itemType == TradeItemType::Player && $trade->partyOneReceives[$k]->player->id == $partyTwoPlayers[$i]->player->id) {
              $foundIt = true;
              break;
           }
        }
        if (!foundIt) {
	   $result .= "<option value='" . $partyTwoPlayers[$i]->player->id . "'>" . $partyTwoPlayers[$i]->player->getDescription() . "</option>";  
        }
}
$result .= getSpacerOption() . "</select></td></tr><tr><td><b>Picks</b></td></tr><tr><td><select id='partyTwoPicks' size='10' multiple>";
$count = count($partyTwoPicks);
for ($i = 0; $i < $count; $i++) {
        $foundIt = false;
        for ($k = 0; k < $count2; k++) {
           if ($trade->partyOneReceives[$k]->itemType == TradeItemType::Pick && $trade->partyOneReceives[$k]->pickNumber == $partyTwoPlayers[$i]->pickNumber) {
              $foundIt = true;
              break;
           }
        }
        if (!foundIt) {
	   $result .= "<option value='" . $partyTwoPicks[$i]->pickNumber . "'>#" . $partyTwoPicks[$i]->pickNumber . "</option>";  
        }
}
$result .= getSpacerOption() . "</select></td></tr><tr><td><b>Receives</b></td></tr><tr><td><select id='partyTwoReceives' size='10' multiple>";
$count = count($trade->partyTwoReceives);
for ($i = 0; $i < $count; $i++) {
   if ($trade->partyTwoReceives[$i]->itemType == TradeItemType::Pick) {
      $result .= "<option value='" . $trade->partyTwoReceives[$i]->pickNumber . "'>#" . $trade->partyTwoReceives[$i]->pickNumber . "</option>";  
   }
   else {
      $result .= "<option value='" . $partyTwoReceives[$i]->player->id . "'>" . $partyTwoReceives[$i]->player->getDescription() . "</option>";  
   }
}
$result .= getSpacerOption() . "</select></td></tr></table></td></tr>";
$result .= "<tr><td colspan='2'><input type='hidden' name='partyOneReceives'><input type='hidden' name='partyTwoReceives'><input type='button' value='Clear' onClick='clearTrade()' class='buttonStyle'>&nbsp;<input type='button' value='Cancel Trade' onClick='cancelTrade()' class='buttonStyle'>&nbsp;<input type='button' value='Propose Counter' onClick='validateTrade(this.form)'></td></tr></table></form><form id='cancelForm' name='cancelForm' action='../trades/cancelTrade.php' method='post' target='hiddenFrame'></form>";

mysql_close();

function getSpacerOption() {
	return "<option>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
			"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
			"&nbsp;&nbsp;&nbsp;</option>";
}
?>

<script>
parent.document.getElementById("tradeDiv").innerHTML = "<?echo $result?>";
parent.document.getElementById("saveTradeHTML").value = "<?echo $result?>"; 
</script>